<?php

namespace app\admin\model;

use think\facade\Cache;
use think\Model;

/**
 * 自定义帖子用户表
 * @package app\admin\model
 */
class CustomPostUsers extends Model
{
    # 设置当前模型对应的完整数据表名称
    protected $name = 'admin_custom_post_users';

    # 自动写入时间戳
    protected $autoWriteTimestamp = true;

    /**
     * 获取缓存键
     * @param $keys
     * @return string
     */
    public function cacheKey($keys)
    {
        return md5($keys.Cache::get($this->name,'cache_PostUsers'));
    }

    /**
     * 更新缓存键
     * @return bool
     */
    public function updateCacheKey()
    {
        return Cache::set($this->name,'cache_PostUsers',time());
    }

    /**
     * 获取数据
     * @return array|mixed|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAll()
    {
        $cacheKey = self::cacheKey('getAll');
        $all = Cache::get($cacheKey);
        if (!$all) {
            $all = self::select();
            $all = $all ? $all->toArray() : [];
            if ($all) {
                Cache::set($cacheKey, $all, 7200);
            }
        }
        return $all;
    }

}